package com.vitabuybuy.tv.ui.shopcenter.store

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.library.common.base.BaseFragment
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentStoreClassificationBinding
import com.vitabuybuy.tv.dto.ClassificationDto
import com.vitabuybuy.tv.ui.shopcenter.CateSearchGoodsResultActivity
import com.vitabuybuy.tv.ui.shopcenter.SearchGoodsOrStoreActivity
import com.vitabuybuy.tv.ui.shopcenter.store.adapter.ShopClassificationListAdapter
import com.vitabuybuy.tv.ui.shopcenter.store.viewmodel.StoreHomeViewModel
import kotlinx.android.synthetic.main.fragment_store_classification.*

class StoreClassificationFragment :
    BaseFragment<StoreHomeViewModel, FragmentStoreClassificationBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_store_classification

    private val shopClassificationListAdapter by lazy { ShopClassificationListAdapter() }
    private var storeId :Int?= -1

    override fun initView(savedInstanceState: Bundle?) {
        recyclerView.apply {
            adapter = shopClassificationListAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
        storeId =  (activity as StoreHomeActivity).getStoreId()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        viewModel.getClassification(storeId)
    }

    override fun observeData() {
        super.observeData()
        viewModel.categoryList.observe(this, Observer {
            shopClassificationListAdapter.setNewData(it)
        })
    }

    override fun eventHandling() {
        super.eventHandling()
        etSearch.setOnClickListener {
            SearchGoodsOrStoreActivity.openActivity(mContext, storeId)
        }
        shopClassificationListAdapter.onTagClick = {
            val bean = ClassificationDto.ChildrenBean.ChildrenBean2()
            bean.id = it.id
            bean.name = it.name
            bean.image = it.image
            bean.position = it.position
            CateSearchGoodsResultActivity.openActivity(
                mContext,
                bean,
                storeId,
                null,
                null
            )
        }
    }
}
